package com.lijie.ljblog.controller;


import com.github.pagehelper.PageInfo;
import com.lijie.ljblog.model.Blog;
import com.lijie.ljblog.service.BlogService;
import com.lijie.ljblog.util.Result;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijie
 * @since 2021-08-28
 */
@RestController
@RequestMapping("/ljblog/blog")
public class BlogController {
    @Resource
    private BlogService blogService;

    @RequestMapping("saveOrUpdateBlog")
    public Result<Object> saveOrUpdateBlog(@RequestBody Blog blog){
        Result<Object> result=new Result<>(Result.STATUS_CODE_500);
        boolean save = blogService.saveOrUpdate(blog);
        if(save){
            result.setSuccess();
        }
        result.setData(blog.getId());
        return result;
    }
    @RequestMapping("/getBlogList/{page}")
    public Result<PageInfo<Blog>> getBlogList(@PathVariable Integer page,  @RequestBody Blog blog){
        Result<PageInfo<Blog>> result=new Result<PageInfo<Blog>>();
        PageInfo<Blog> blogPageInfo = blogService.selBlogPage(page, blog);
        result.setStatus(Result.STATUS_CODE_SUCCESS);
        result.setData(blogPageInfo);
        return result;
    }
    @RequestMapping("/getNotice/{page}")
    public Result<PageInfo<Blog>> getNotice(@PathVariable Integer page,  @RequestBody Blog blog){
        Result<PageInfo<Blog>> result=new Result<PageInfo<Blog>>();
        PageInfo<Blog> blogPageInfo = blogService.selNotice(page, blog);
        result.setStatus(Result.STATUS_CODE_SUCCESS);
        result.setData(blogPageInfo);
        return result;
    }
    @RequestMapping("/selBlogById")
    public Result<Blog> selBlogById( @RequestBody Blog blog){
        Result<Blog> result=new Result<>();
        if(blog.getId()!=null){
            result= blogService.getById(blog.getId());
        }else{
            result.setStatus(Result.STATUS_CODE_500);
            result.setMessage("博客ID为空");
        }
        return result;

    }
    @RequestMapping("/getByIdAndAddReadNums")
    public Result<Blog> getByIdAndAddReadNums( @RequestBody Blog blog){
        Result<Blog> result=new Result<>();
        if(blog.getId()!=null){
            result= blogService.getByIdAndAddReadNums(blog.getId());
            result.setSuccess();
        }else{
            result.setStatus(Result.STATUS_CODE_500);
            result.setMessage("博客ID为空");
        }
        return result;
    }

    @RequestMapping("/hotBlog")
    public Result<List<Blog>> hotBlog(){
       return blogService.hotBlog();
    }
}

